package com.todo.user.application.convert;

import com.google.common.collect.Lists;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.GenderEnum;
import com.todo.common.common.enums.SourceEnum;
import com.todo.user.application.entity.bo.FollowFriendInfoBO;
import com.todo.user.application.entity.bo.UserAccountBO;
import com.todo.user.application.entity.bo.UserAccountConditionBO;
import com.todo.user.application.entity.bo.UserInfoDetailBO;
import com.todo.user.common.enums.*;
import com.todo.user.infrastructure.entity.condition.UserAccountCondition;
import com.todo.user.infrastructure.entity.domain.UserAccountDO;
import com.todo.user.infrastructure.entity.po.UserAccountPO;
import com.todo.user.infrastructure.entity.po.UserRelationPO;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhangwenxiang
 * @date 2024/4/8 10:31
 * @description
 */
public class UserServiceConvert {

    public static UserAccountBO convertToUserAccountBO(UserAccountPO userAccountPO) {
        if (userAccountPO == null) {
            return null;
        }
        return UserAccountBO.builder()
                .birthday(userAccountPO.getBirthday())
                .gender(userAccountPO.getGender())
                .isDelete(userAccountPO.getIsDelete())
                .isMobileVerify(userAccountPO.getIsMobileVerify())
                .isReal(userAccountPO.getIsReal())
                .location(userAccountPO.getLocation())
                .nativePlace(userAccountPO.getNativePlace())
                .status(UserStatusEnum.getByType(userAccountPO.getStatus()))
                .source(SourceEnum.getByType(userAccountPO.getSource()))
                .updateTime(userAccountPO.getUpdateTime())
                .backgroundImg(userAccountPO.getBackgroundImg())
                .headImg(userAccountPO.getHeadImg())
                .mobileNationCode(userAccountPO.getMobileNationCode())
                .mobile(userAccountPO.getMobile())
                .nickName(userAccountPO.getNickName())
                .signature(userAccountPO.getSignature())
                .userName(userAccountPO.getUserName())
                .password(userAccountPO.getPassword())
                .userType(userAccountPO.getUserType())
                .id(userAccountPO.getId())
                .createTime(userAccountPO.getCreateTime())
                .career(userAccountPO.getCareer())
                .build();
    }

    public static PageResult<FollowFriendInfoBO> convertToPageFollowFriendInfoBO(PageResult<UserRelationPO> userRelationPOPageResult) {
        if (userRelationPOPageResult == null) {
            return PageResult.of(Lists.newArrayList(), 0, 0, 0);
        }
        if (CollectionUtils.isEmpty(userRelationPOPageResult.getResults())) {
            return PageResult.of(Lists.newArrayList(), userRelationPOPageResult.getPageNum(), userRelationPOPageResult.getPageSize(), userRelationPOPageResult.getTotal());
        }
        PageResult<FollowFriendInfoBO> result = new PageResult<>();
        result.setPageNum(userRelationPOPageResult.getPageNum());
        result.setPageSize(userRelationPOPageResult.getPageSize());
        result.setTotal(userRelationPOPageResult.getTotal());
        List<FollowFriendInfoBO> collect = userRelationPOPageResult
                .getResults()
                .stream()
                .map(UserServiceConvert::convertToFollowFriendInfoBO)
                .collect(Collectors.toList());
        result.setCurrentIndex(userRelationPOPageResult.getCurrentIndex());
        result.setResults(collect);
        return result;
    }

    public static UserAccountDO convertToUserAccountDO(UserInfoDetailBO userInfoDetailBO) {
        if (userInfoDetailBO == null) {
            return null;
        }
        UserAccountDO userAccountDO = new UserAccountDO();
        userAccountDO.setId(userInfoDetailBO.getUserId());
        userAccountDO.setIsDelete(userAccountDO.getIsDelete());
        userAccountDO.setCreateTime(userAccountDO.getCreateTime());
        userAccountDO.setUpdateTime(userAccountDO.getUpdateTime());
        userAccountDO.setBaseAccountInfo(buildBaseAccountInfo(userInfoDetailBO));
        userAccountDO.setAccountDetail(buildAccountDetail(userInfoDetailBO));
        return userAccountDO;
    }

    private static UserAccountDO.AccountDetail buildAccountDetail(UserInfoDetailBO userInfoDetailBO) {
        UserAccountDO.AccountDetail accountDetail = new UserAccountDO.AccountDetail();
        accountDetail.setHolidayType(userInfoDetailBO.getHolidayType().getType());
        accountDetail.setMbit(userInfoDetailBO.getMbit());
        accountDetail.setInterestList(userInfoDetailBO.getInterestList());
        accountDetail.setPromotion(userInfoDetailBO.getPromotion());
        accountDetail.setTravelObjective(userInfoDetailBO.getTravelObjective());
        accountDetail.setTravelPlanType(userInfoDetailBO.getTravelPlanType() == null ? null : userInfoDetailBO.getTravelPlanType().getType());
        return accountDetail;
    }

    private static UserAccountDO.BaseAccountInfo buildBaseAccountInfo(UserInfoDetailBO userInfoDetailBO) {
        UserAccountDO.BaseAccountInfo baseAccountInfo = new UserAccountDO.BaseAccountInfo();
        baseAccountInfo.setGender(userInfoDetailBO.getGender() == null ? null : userInfoDetailBO.getGender().getType());
        baseAccountInfo.setLocation(userInfoDetailBO.getLocation());
        baseAccountInfo.setNickName(userInfoDetailBO.getNickName());
        baseAccountInfo.setNativePlace(userInfoDetailBO.getNativePlace());
        baseAccountInfo.setSignature(userInfoDetailBO.getSignature());
        baseAccountInfo.setSource(userInfoDetailBO.getSource() == null ? null : userInfoDetailBO.getSource().getType());
        baseAccountInfo.setUserType(userInfoDetailBO.getUserType());
        baseAccountInfo.setHeadImg(userInfoDetailBO.getHeadImg());
        baseAccountInfo.setBackgroundImg(userInfoDetailBO.getBackgroundImg());
        baseAccountInfo.setBirthday(userInfoDetailBO.getBirthday());
        baseAccountInfo.setCareer(userInfoDetailBO.getCareer());
        return baseAccountInfo;
    }

    public static UserInfoDetailBO convertToUserInfoDetailBO(UserAccountDO userAccountDO) {
        if (userAccountDO == null) {
            return null;
        }
        if (userAccountDO.getAccountDetail() == null) {
            return UserInfoDetailBO.builder()
                    .userId(userAccountDO.getId())
                    .userName(userAccountDO.getBaseAccountInfo().getUserName())
                    .backgroundImg(userAccountDO.getBaseAccountInfo().getBackgroundImg())
                    .birthday(userAccountDO.getBaseAccountInfo().getBirthday())
                    .career(userAccountDO.getBaseAccountInfo().getCareer())
                    .gender(GenderEnum.getGenderEnumByType(userAccountDO.getBaseAccountInfo().getGender()))
                    .headImg(userAccountDO.getBaseAccountInfo().getHeadImg())
                    .location(userAccountDO.getBaseAccountInfo().getLocation())
                    .nativePlace(userAccountDO.getBaseAccountInfo().getNativePlace())
                    .nickName(userAccountDO.getBaseAccountInfo().getNickName())
                    .signature(userAccountDO.getBaseAccountInfo().getSignature())
                    .source(UserSourceEnum.getByType(userAccountDO.getBaseAccountInfo().getSource()))
                    .userStatus(UserStatusEnum.getByType(userAccountDO.getBaseAccountInfo().getStatus()))
                    .userType(userAccountDO.getBaseAccountInfo().getUserType())
                    .createTime(userAccountDO.getCreateTime())
                    .updateTime(userAccountDO.getUpdateTime())
                    .build();
        }

        return UserInfoDetailBO.builder()
                .userId(userAccountDO.getId())
                .backgroundImg(userAccountDO.getBaseAccountInfo().getBackgroundImg())
                .birthday(userAccountDO.getBaseAccountInfo().getBirthday())
                .career(userAccountDO.getBaseAccountInfo().getCareer())
                .gender(GenderEnum.getGenderEnumByType(userAccountDO.getBaseAccountInfo().getGender()))
                .headImg(userAccountDO.getBaseAccountInfo().getHeadImg())
                .holidayType(userAccountDO.getAccountDetail().getHolidayType() == null ? null : HolidayTypeEnum.getByType(userAccountDO.getAccountDetail().getHolidayType()))
                .interestList(userAccountDO.getAccountDetail() == null ? null : userAccountDO.getAccountDetail().getInterestList())
                .location(userAccountDO.getBaseAccountInfo().getLocation())
                .mbit(userAccountDO.getAccountDetail() == null ? null : userAccountDO.getAccountDetail().getMbit())
                .nativePlace(userAccountDO.getBaseAccountInfo().getNativePlace())
                .nickName(userAccountDO.getBaseAccountInfo().getNickName())
                .promotion(userAccountDO.getAccountDetail() == null ? null : userAccountDO.getAccountDetail().getPromotion())
                .signature(userAccountDO.getBaseAccountInfo().getSignature())
                .source(UserSourceEnum.getByType(userAccountDO.getBaseAccountInfo().getSource()))
                .userStatus(UserStatusEnum.getByType(userAccountDO.getBaseAccountInfo().getStatus()))
                .travelObjective(userAccountDO.getAccountDetail() == null ? null : userAccountDO.getAccountDetail().getTravelObjective())
                .travelPlanType(userAccountDO.getAccountDetail().getTravelPlanType() == null ? null : TravelPlanTypeEnum.getByType(userAccountDO.getAccountDetail().getTravelPlanType()))
                .userType(userAccountDO.getBaseAccountInfo().getUserType())
                .createTime(userAccountDO.getCreateTime())
                .updateTime(userAccountDO.getUpdateTime())
                .build();
    }

    public static FollowFriendInfoBO convertToFollowFriendInfoBO(UserRelationPO userRelationPO) {
        if (userRelationPO == null) {
            return null;
        }
        return FollowFriendInfoBO.builder()
                .createTime(userRelationPO.getCreateTime())
                .friendType(FriendTypeEnum.getByType(userRelationPO.getRelationType()))
                .operateReason(userRelationPO.getOperateReason())
                .relationUserId(userRelationPO.getRelationUserId())
                .userId(userRelationPO.getUserId())
                .build();
    }

    public static UserAccountCondition convertToUserAccountCondition(UserAccountConditionBO conditionBO) {
        if (conditionBO == null) {
            return null;
        }
        UserAccountCondition condition = new UserAccountCondition();
        condition.setUserId(conditionBO.getUserId());
        condition.setPageSize(conditionBO.getPageSize());
        condition.setUserName(conditionBO.getUserName());
        condition.setStatus(conditionBO.getStatus());
        condition.setLocation(conditionBO.getLocation());
        condition.setMobile(conditionBO.getMobile());
        return condition;
    }

    public static PageResult<UserAccountBO> convertToPageUserAccountBO(PageResult<UserAccountPO> userAccountPOPageResult) {
        if (userAccountPOPageResult == null) {
            return PageResult.of(Lists.newArrayList(), 0, 0, 0);
        }
        PageResult<UserAccountBO> pageResult = new PageResult<>();
        pageResult.setCurrentIndex(userAccountPOPageResult.getCurrentIndex());
        pageResult.setPageCount(userAccountPOPageResult.getPageCount());
        pageResult.setPageSize(userAccountPOPageResult.getPageSize());
        pageResult.setTotal(userAccountPOPageResult.getTotal());
        pageResult.setResults(userAccountPOPageResult.getResults().stream().map(UserServiceConvert::convertToUserAccountBO).collect(Collectors.toList()));
        return pageResult;
    }
}
